package com.yan.shopping.service;

import com.yan.shopping.dao.DateProduct;
import com.yan.shopping.domain.User;

import java.util.Scanner;

public class ShoppingServicesimpl implements ShoppingServices{

    public static final Scanner INPUT = new Scanner(System.in);
    @Override
    public User logIn() {
        System.out.println("请输入用户名");
        StringBuffer Uname = new StringBuffer();
        Uname.append(INPUT.next());
        System.out.println("输入密码");
        StringBuffer UpassWord = new StringBuffer();
        UpassWord.append(INPUT.next());
        for (int i = 0; i < DateProduct.userList.size(); i++) {
            if(Uname.toString().equals(DateProduct.userList.get(i).getName().toString())){
                if(UpassWord.toString().equals(DateProduct.userList.get(i).getPassWord().toString())){
                    System.out.println("登录成功, 你好" + Uname);
                    return DateProduct.userList.get(i);
                }else{
                    System.out.println("用户名和密码错误");
                }
            }else{
                System.out.println("用户名和密码错误");
            }
        }
        return null;
    }

    @Override
    public void signIn() {
        User user = new User();
        System.out.println("请输入用户名");
        StringBuffer name = new StringBuffer();
        name.append(INPUT.next());
        user.setName(name);
        while (true) {
            System.out.println("请输入密码");
            StringBuffer passWord = new StringBuffer();
            passWord.append(INPUT.next());
            while(passWord.length() < 6){
                System.out.println("密码不能小于6位");
                System.out.println("请在次输入密码");
                passWord.delete(0 , name.length());
                passWord.append(INPUT.next());
            }
            System.out.println("请确认密码");
            StringBuffer passWord2 = new StringBuffer();
            passWord2.append(INPUT.next());
            if(passWord.toString().equals(passWord2.toString())){
               user.setPassWord(passWord);
               break;
            }else{
                System.out.println("两次密码不一样");
            }
        }
        System.out.println("请输入年龄");
        Integer age = INPUT.nextInt();
        while(age < 0 || age > 100){
            System.out.println("输入的年龄必须在0-100之间");
            System.out.println("请重新输入");
            age = INPUT.nextInt();
        }
        user.setAge(age);
        System.out.println("请输入昵称");
        StringBuffer otherName = new StringBuffer(INPUT.next());
        user.setOtherName(otherName);
        DateProduct.userList.add(user);
        System.out.println("注册成功");
    }

    @Override
    public void quit() {

    }

    @Override
    public Integer Checking(StringBuffer name) {
        Integer index = -1;
       for (int i = 0; i < DateProduct.userList.size(); i++) {
            if(DateProduct.userList.get(i).getName().equals(name)){
                index = i;
            }
        }
        return index;
    }

}
